import EventCenter from "../../lib/EventCenter";
import EventEunm from "../data/EventEunm";
import ItemBaseData from "../data/ItemBaseData";
import GridManager from "../GridManager";

export default class PaoFireCtr {
    private mBody: Laya.Sprite;
    private mFloorPos = { x: 0, y: 10 }
    private mIconBgPos = [{ x: 98, y: 50, width: 60 }, { x: 103, y: 36, width: 50 }];
    private mPos = [{ x: 80, y: 0, width: 60 }, { x: 129, y: 81, width: 60 }];
    private mPos2 = [{ x: 80, y: 0, width: 60 }, { x: 128, y: 44, width: 55 }, { x: 112, y: 83, width: 35 }, { x: 82, y: 97.5, width: 30 }];

    private mPosNum: number = 2;
    private mPaoNum: number = 0;
    private changeTime: number = 0;
    private mIsWujiMode: boolean;
    private mHaveTempIcon: boolean;
    private mIcons = [];
    constructor(_body: Laya.Sprite) {
        this.mBody = _body;
    }

    public show(_vale: boolean) {
        this.mBody.visible = _vale;
    }

    public addPos(num: number) {
        let ttemp: number = this.mPosNum;
        this.mPosNum += num;
        let tlen = this.mIcons.length;
        let ticon
        for (let i = 0; i < this.mPosNum; i++) {
            if (i < tlen) {
                ticon = this.mIcons[i]
                ticon.move(this.getPosByIndex(i), false);
            } else {
                if (i >= this.paoNum - 1) {
                    ticon = this.createIcon()
                    ticon.setData(this.getNextPao());
                    ticon.move(this.getPosByIndex(this.mPosNum + i));
                    this.mBody.addChild(ticon);
                    ticon.on(Laya.Event.CLICK, this, this.change);
                    this.mIcons[i] = ticon;
                }
            }
        }
        this.freshItemBgPos();
    }

    public setPaoNum(_num: number, _isWuJi: boolean = false) {
        this.mIsWujiMode = _isWuJi;
        this.mPaoNum = _num;
        this.mPosNum = 2;
        this.mHaveTempIcon = false;
        this.clearPropData();
        this.mBody.getChildByName("stepTxt")["visible"] = this.mBody.getChildByName("numBg")["visible"] = !_isWuJi;
        this.createIcons(this.mPosNum);
        this.freshView();
    }

    public addPaoNum(value: number) {
        this.mPaoNum += value;
        if (this.mIcons.length < this.mPosNum) {
            let tlen = this.mIcons.length;
            let ticon: Icon;
            for (let i = 0; i < this.mPosNum; i++) {
                if (i < tlen) {
                    ticon = this.mIcons[i];
                    ticon.move(this.getPosByIndex(i));
                } else {
                    if (i < this.paoNum) {
                        ticon = this.createIcon();
                        this.mBody.addChild(ticon);
                        ticon.setData(this.getNextPao());
                        ticon.move(this.getPosByIndex(i));
                        this.mIcons[i] = ticon;
                    }
                }
            }
        }
        this.freshView();
    }

    public get paoNum() {
        return this.mPaoNum;
    }


    public fire() {
        if (this.isMove) return null;
        let ticon: Icon = this.getFristIcon();
        let tdata = ticon.currData;

        return tdata;
    }

    public createNextData() {
        let ticon: Icon = this.getFristIcon();
        if (this.mPaoNum > this.mIcons.length) {
            ticon.setData(this.getNextPao());
            this.moveTrun()
        } else {
            this.mIcons.shift();
            for (let i = 0; i < this.mIcons.length; i++) {
                this.mIcons[i].move(this.getPosByIndex(i), true);
            }
        }
        if (!this.mIsWujiMode && !this.mHaveTempIcon) {
            this.mPaoNum--;
            this.freshView();
        }
    }

    public change() {
        if (this.mHaveTempIcon) {
            EventCenter.getIns().event(EventEunm.canceUseDaoJu);
            return;
        }
        if (this.isMove) return;
        let time = new Date().getTime();
        if (time - this.changeTime < 200) return;
        this.changeTime = time;
        this.moveTrun();
    }

    public get posNum() {
        return this.mPosNum;
    }

    public get currData() {
        let ticon: Icon = this.getFristIcon();
        return ticon.currData;
    }
    public set currData(_data: ItemBaseData) {
        let ticon: Icon = this.getFristIcon();
        ticon.setData(_data);
    }

    public setPropData(data) {
        this.mHaveTempIcon = true;
        let ticon: Icon = this.getFristIcon();
        ticon.setPropData(data);
        this.showEff(this.mHaveTempIcon);
    }

    public clearPropData() {
        for (let i = 0; i < this.mIcons.length; i++) {
            this.mIcons[i].clearPropData();
        }
        this.mHaveTempIcon = false;
        this.showEff(this.mHaveTempIcon);
    }

    public subPao() {
        //console.log("subPao",this.mPaoNum,this.mPaoNum-1);
        this.mPaoNum--;
        this.freshView();
    }

    public get isMove() {
        for (let i = 0; i < this.mIcons.length; i++) {
            if (this.mIcons[i].isMove) return true;
        }
        return false;
    }

    private showEff(show: boolean) {
        this.mBody.getChildByName("dirSp").getChildByName("eff")["visible"] = show;
    }

    private createIcons(num: number) {
        this.clearIcons();
        let ticon: Icon;
        for (let i = 0; i < num; i++) {
            ticon = this.createIcon();
            this.mBody.addChild(ticon);
            ticon.setData(this.getNextPao());
            ticon.move(this.getPosByIndex(i));
            this.mIcons[i] = ticon;
        }
        this.freshItemBgPos();
    }

    public clearIcons() {
        for (let i = 0; i < this.mIcons.length; i++) {
            this.recoverIcon(this.mIcons[i]);
        }
        this.mIcons = [];
    }

    private moveTrun() {
        let tnextIndex: number;
        let tposdata;
        let temp: Icon;
        for (let i = 0; i < this.mIcons.length; i++) {
            tnextIndex = i;
            if (i == this.mIcons.length - 1) tnextIndex = 0;
            temp = this.mIcons[i];
            this.mIcons[i] = this.mIcons[tnextIndex];
            this.mIcons[tnextIndex] = temp;
        }
        for (let i = 0; i < this.mIcons.length; i++) {
            tposdata = this.getPosByIndex(i);
            this.mIcons[i].move(tposdata, true);
        }
    }

    private freshView() {
        this.mBody.getChildByName("stepTxt")["text"] = this.mPaoNum;
    }

    private freshItemBgPos() {
        let tbg: any = this.mBody.getChildByName("iconBg");
        let tindex = this.mPosNum > 2 ? 1 : 0;
        let tposdata = this.mIconBgPos[tindex];
        tbg.pos(tposdata.x, tposdata.y);
        tbg.width = tbg.height = tposdata.width;
    }

    private getNextPao(): ItemBaseData {
        let tids = GridManager.getIns().grid.getPaoIds();
        if (this.mIsWujiMode && tids.length <= 0) {
            tids.push(GridManager.getIns().getPaoId(1));
        }
        let tpaoid = tids[Math.floor(Math.random() * tids.length)];
        let tdata: ItemBaseData = new ItemBaseData();
        tdata.setDataById(tpaoid);
        return tdata;
    }

    private getPosByIndex(_index: number) {
        let tposdata;
        if (this.mPosNum > 2) {
            tposdata = this.mPos2[_index];
        } else {
            tposdata = this.mPos[_index];
        }
        return tposdata;
    }

    private getFristIcon(): Icon {
        return this.mIcons[0];
    }

    public createIcon() {
        let ticon: Icon = Laya.Pool.getItem("icons");
        if (!ticon) ticon = new Icon();
        ticon.on(Laya.Event.CLICK, this, this.change);
        return ticon
    }

    public recoverIcon(icon: Icon) {
        icon.removeSelf();
        Laya.Pool.recover("icons", icon);
    }

}

class Icon extends Laya.Image {

    private mdata: ItemBaseData;
    private mPropData: ItemBaseData;
    private mIsMove: boolean;
    constructor() {
        super();
        this.anchorX = this.anchorY = 0.5;
    }

    public setData(_data: ItemBaseData) {
        this.mdata = _data;
        this.visible = _data ? true : false;
        if (_data) this.skin = _data.icon;
        this.mIsMove = false;
    }

    public setPropData(data: ItemBaseData) {
        this.mPropData = data;
        this.skin = this.mPropData.icon;
    }

    public clearPropData() {
        this.mPropData = null;
        this.skin = this.mdata.icon;
    }

    public move(posdata, tw: boolean = false, call: Function = null) {
        if (tw) {
            this.mIsMove = true;
            Laya.Tween.to(this, { x: posdata.x, y: posdata.y, width: posdata.width, height: posdata.width }, 200, Laya.Ease.circOut, new Laya.Handler(this, () => {
                this.mIsMove = false;
                if (call) call();
            }));
        } else {
            this.pos(posdata.x, posdata.y);
            this.width = this.height = posdata.width;
            if (call) call();
        }
    }


    public get currData() {
        if (this.mPropData) return this.mPropData;
        return this.mdata;
    }

    public clear() {
        this.mdata = null;
    }

    public get isMove() {
        return this.mIsMove;
    }

}